package com.genITeam.ria.actions;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.genITeam.ria.bl.MemberBL;
import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.utility.Utility;
import com.genITeam.ria.vo.Buddy;
import com.genITeam.ria.vo.Member;
import com.genITeam.ria.vo.ResponseVo;

public class MemberAction extends AbstractAction {

	public MemberAction() {

		BasicConfigurator.configure();

	}

	Logger logger = Logger.getLogger(MemberAction.class);

	/**
	 * handleCreateMemeber to get Parameters from Request object and write
	 * resulting response
	 * 
	 * 
	 * @param HttPServletRequest,HttpServletResponse
	 * @return none
	 * @throws NGFException
	 */

	public void handleCreateMember(HttpServletRequest request,
			HttpServletResponse response) throws NGFException {

		MemberBL memberBL = null;

		try {
			memberBL = new MemberBL();
			System.out.println("start create member");
			logger.info("start handleCreateMember");
			Member memberVo = new Member();
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String email = request.getParameter("email");
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			// String phoneNumber = request.getParameter("phoneNumber");
			// String address = request.getParameter("address");

			System.out.println(userName + ": in Action");
			// threadPostVo.setPostId(1);
			memberVo.setUserName(userName);
			memberVo.setPassword(password);
			memberVo.setFirstName(firstName);
			memberVo.setLastName(lastName);
			memberVo.setEmail(email);
			memberVo.setJoinedOn(Utility.getCurrentSQLDate());
			memberVo.setIsBan(new Character('F'));
			memberVo.setRole('N');
			memberVo.setNick("Guest");
			// memberVo.userName;
			// memberVo.setPhone(phoneNumber);
			// memberVo.setAddress(address);

			ResponseVo responseVo = new ResponseVo();
			responseVo = memberBL.createUser(memberVo);

			this.initAction(request, response);
			this
					.writeResponse("<error>" + responseVo.getMessage()
							+ "</error>");
			// this.handleAddBuddy(request, response);

		} catch (Exception e) {
			logger.error("Exception=" + e.toString());

			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}

	}

	/**
	 * handleAddBuddy to get Parameters from Request object and write resulting
	 * response
	 * 
	 * 
	 * @param HttPServletRequest,HttpServletResponse
	 * @return none
	 * @throws NGFException
	 */

	public void handleAddBuddy(HttpServletRequest request,
			HttpServletResponse response) throws NGFException {

		MemberBL memberBL = new MemberBL();

		try {
			logger.info("start handleAddBuddy");

			Buddy buddy = new Buddy(); // new buddy obj is created here

			Member member = new Member();
			String memberId = request.getParameter("memberId"); // to extract
			// the member
			// from the
			// request
			String buddyId = request.getParameter("buddyId"); // to extract
			// the buddyId

			member.setId(Integer.parseInt(buddyId));

			buddy.setMemberId(new Integer(memberId));
			buddy.setMember(member);

			memberBL.addBuddy(buddy);

			this.initAction(request, response);
			this.writeResponse("<message>Member Added as Buddy</message>");

		} catch (NGFException e) {
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getErrorMessage() + "</error>");
			System.out.print("Search Forum Excepton = " + e.getMessage());
		} catch (Exception e) {

			logger.error("Exception=" + e.toString());

			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}

	}

	/**
	 * handleviewProfile to get Parameters from Request object and write
	 * resulting response
	 * 
	 * 
	 * @param HttPServletRequest,HttpServletResponse
	 * @return none
	 * @throws NGFException
	 */
	public void handleviewProfile(HttpServletRequest request,
			HttpServletResponse response) throws NGFException {

		// HomeBL homeBL = new HomeBL();
		MemberBL memberBL = new MemberBL();

		try {
			logger.info("start hanldeviewProfile");
			String memberId = request.getParameter("memberId");
			System.out.println("handleviewProfile memberId= " + memberId);
			this.initAction(request, response);
			this.writeResponse(memberBL.generateMemberData(memberId));
			// System.out.println("\n\nWE ARE
			// TESTING\n\n"+memberBL.generateMemberData(memberId));
		} catch (NGFException e) {
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getErrorMessage() + "</error>");
			System.out.print("Search Forum Excepton = " + e.getMessage());
		} catch (Exception e) {
			logger.info("Exception=" + e.toString());
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}

	}

	/**
	 * handleUpdateProfile Parameters from Request object and write resulting
	 * response
	 * 
	 * 
	 * @param HttPServletRequest,HttpServletResponse
	 * @return none
	 * @throws NGFException
	 */

	public void handleUpdateProfile(HttpServletRequest request,

	HttpServletResponse response) throws NGFException {

		MemberBL memberBL = new MemberBL();

		try {
			logger.info("start handleUpdateProfile");
			String memberId = request.getParameter("memberId");
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String address = request.getParameter("address");
			String joinedOn = request.getParameter("joinedOn");
			String aboutMe = request.getParameter("aboutMe");
			String degree = request.getParameter("degree");
			String degreeYear = request.getParameter("degreeYear");
			String college = request.getParameter("college");
			String careerInterests = request.getParameter("careerInterests");
			String homePage = request.getParameter("homePage");
			String education = request.getParameter("education");
			String major = request.getParameter("major");
			System.out.println("handleviewProfile memberId= " + memberId);

			this.initAction(request, response);

			Member member = new Member();

			member = memberBL.getMemberData(memberId);
			member.setFirstName(firstName);
			member.setLastName(lastName);
			member.setEmail(email);
			member.setPhone(phone);
			member.setAddress(address);
			member.setJoinedOn(com.genITeam.ria.utility.Utility
					.convertStringToDate(joinedOn, "dd-MM-yyyy hh:mm:ss a"));
			member.setAboutMe(aboutMe);
			member.setDegree(degree);
			member.setDegreeYear(degreeYear);
			member.setCollege(college);
			member.setCareerInterests(careerInterests);
			member.setHomePage(homePage);
			member.setEducation(education);
			member.setMajor(major);
			memberBL.updateUser(member);
			this.writeResponse(memberBL.generateMemberData(memberId));
			//this.writeResponse("<error>" + " " + "</error>");

		} catch (NGFException e) {
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getErrorMessage() + "</error>");
			System.out.print("Search Forum Excepton = " + e.getMessage());
		} catch (Exception e) {

			logger.error("Exception=" + e.toString());
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}

	}

	public void handleBanMember(HttpServletRequest request,

	HttpServletResponse response) throws NGFException {

		// int memId = Integer.parseInt(request.getParameter("memberid"));
		String memId = request.getParameter("memberid");
		System.out.println("member id  =  = = = = = = = == = " + memId);
		Member memberVo = new Member();
		MemberBL memberBL = new MemberBL();
		Session session = null;
		try {
			memberVo = memberBL.getMemberData(memId);

			String fName = memberVo.getFirstName();
			String lName = memberVo.getLastName();
			String nick = memberVo.getNick();
			String email = memberVo.getEmail();
			String phone = memberVo.getPhone();
			String address = memberVo.getAddress();
			Date joinedOn = memberVo.getJoinedOn();
			String userName = memberVo.getUserName();
			String password = memberVo.getPassword();
			String edutcation = memberVo.getEducation();
			String college = memberVo.getCollege();
			String major = memberVo.getMajor();
			String degree = memberVo.getDegree();
			String degreeYear = memberVo.getDegreeYear();
			String careerInterests = memberVo.getCareerInterests();
			String homepage = memberVo.getHomePage();
			String aboutMe = memberVo.getAboutMe();
			String photo = memberVo.getPicture();
			Character isBan = memberVo.getIsBan();
			char role = memberVo.getRole();

			// System.out.println(memId);
			// System.out.println(nick);
			// System.out.println(phone);

			memberVo.setId(Integer.parseInt(memId));
			memberVo.setIsBan(new Character('t'));
			memberVo.setFirstName(fName);
			memberVo.setLastName(lName);
			memberVo.setNick(nick);
			memberVo.setEmail(email);
			memberVo.setPhone(phone);
			memberVo.setAddress(address);
			memberVo.setJoinedOn(joinedOn);
			memberVo.setUserName(userName);
			memberVo.setPassword(password);
			memberVo.setEducation(edutcation);
			memberVo.setCollege(college);
			memberVo.setMajor(major);
			memberVo.setDegree(degree);
			memberVo.setDegreeYear(degreeYear);
			memberVo.setCareerInterests(careerInterests);
			memberVo.setHomePage(homepage);
			memberVo.setAboutMe(aboutMe);
			memberVo.setPicture(photo);
			memberVo.setRole(role);
			memberBL.banUser(memberVo, session);

		} catch (NGFException e) {
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getErrorMessage() + "</error>");
			System.out.print("Search Forum Excepton = " + e.getMessage());
		} catch (Exception e) {
			logger.error("Exception=" + e.toString());
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}
	}

}
